package io.knifer.gokexceltask.excel.converter;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import io.knifer.gokexceltask.pojo.domain.Student;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class StudentStatusConverter implements Converter<Student.Status> {
    @Override
    public Class<?> supportJavaTypeKey() {
        return Student.Status.class;
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.STRING;
    }

    @Override
    public Student.Status convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        switch (cellData.getStringValue()){
            case "在校":
                return Student.Status.FINE;
            case "辍学":
                return Student.Status.DROPOUT;
            default:
                log.warn("row index {}: unknown student status.", cellData.getRowIndex());
                return Student.Status.UNKNOWN;
        }
    }

    @Override
    public WriteCellData<String> convertToExcelData(Student.Status value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        return new WriteCellData<>(value.getValue());
    }
}
